/*
 * Copyright 2011 Impetus Infotech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.impetus.teamplan.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.impetus.teamplan.Constants;
import com.impetus.teamplan.PlanMonth;
import com.impetus.teamplan.dao.TeamPlanDao;
import com.impetus.teamplan.dao.TeamPlanDaoImpl;
import com.impetus.teamplan.entity.Accomplishment;
import com.impetus.teamplan.entity.Member;

/**
 * <Provide Type description>
 * @author amresh.singh
 */
public class TeamPlanUtils {	
	public static List<Member> getAllMembers(HttpSession session) {
		List<Member> allMembers = (List<Member>)session.getAttribute(Constants.ALL_MEMBERS); 
		if(allMembers != null) {
			return allMembers;
		} else {
			TeamPlanDao teamPlanDao = new TeamPlanDaoImpl();
			allMembers = teamPlanDao.getAllMembers();
			
			for(Member member : allMembers) {
				List<Accomplishment> accomplishments = member.getAccomplishments();
				for(Accomplishment acc : accomplishments) {
					acc.setMonthStr(WebUtils.getMonth(acc.getMonth()) + "'" + acc.getYear());
				}
			}
			session.setAttribute(Constants.ALL_MEMBERS, allMembers);
			return allMembers;
		}		
	}
	
	/**
	 * @param request
	 */
	public static Accomplishment getLatestAccomplishment(HttpServletRequest request) {
		List<Member> allMembers = TeamPlanUtils.getAllMembers(request.getSession());
		List<Accomplishment> accomplishments = allMembers.get(0).getAccomplishments();
		Accomplishment latestAccomplishment = accomplishments.get(accomplishments.size() - 1);
		
		return latestAccomplishment;
	}
	
	public static PlanMonth getNextMonthForPlanning(Member member) {
		List<Accomplishment> accomplishments = member.getAccomplishments();
		PlanMonth pm = null;
		
		if(accomplishments == null || accomplishments.isEmpty()) {
			//Current Month
			pm = PlanMonth.getCurrentMonth();
		} else {
			//1 more than the last one
			Accomplishment latestAccomplishment = getLatestAccomplishment(member.getAccomplishments());
			pm = PlanMonth.getNextMonth(latestAccomplishment.getMonth(), latestAccomplishment.getYear());			
		}
		return pm;
	}
	
	public static Accomplishment getLatestAccomplishment(List<Accomplishment> accs) {
		//return accs.get(accs.size() - 1);
		Accomplishment latestAcc = null;
		int maxMonth = 0;
		int maxYear = 0;
		for(Accomplishment acc : accs) {
			int month = Integer.parseInt(acc.getMonth());
			int year = Integer.parseInt(acc.getYear());
			
			if(year > maxYear) {
				maxYear = year;
				maxMonth = month;
				latestAcc = acc;
			} else if (year == maxYear){
				if(month > maxMonth) {
					maxMonth = month;
					latestAcc = acc;
				}				
			}
		}
		return latestAcc;		
	}
	
	public static void main(String args[]) {
		List<Accomplishment> accs = new ArrayList<Accomplishment>();
		accs.add(new Accomplishment(null, "8", "2010", new ArrayList<String>()));
		accs.add(new Accomplishment(null, "2", "2010", new ArrayList<String>()));
		accs.add(new Accomplishment(null, "1", "1012", new ArrayList<String>()));
		accs.add(new Accomplishment(null, "1", "1011", new ArrayList<String>()));
		accs.add(new Accomplishment(null, "11", "2009", new ArrayList<String>()));
		accs.add(new Accomplishment(null, "5", "2007", new ArrayList<String>()));	
		
		System.out.println(getLatestAccomplishment(accs));
	}
}
